package com.luofan.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.luofan.common.result.Result;
import com.luofan.common.result.ResultCodeEnum;
import com.luofan.entity.po.Term;
import com.luofan.service.TermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学期表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/term")
public class TermController {

    @Autowired
    private TermService termService;

    //获取所有学期信息
    @GetMapping
    public Result<List<Term>> listAllTerm() {
        LambdaQueryWrapper<Term> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Term::getStart);
        List<Term> termList = termService.list(wrapper);
        return Result.ok(termList);
    }

    //根据id获取学期信息
    @GetMapping("/{id}")
    public Result<Term> getTermById(@PathVariable Long id) {
        Term term = termService.getById(id);
        return Result.ok(term);
    }

    //创建新学期信息
    @PostMapping
    public Result<String> createTerm(@RequestBody Term term) {
        ResultCodeEnum resultCodeEnum = termService.createTerm(term);
        return Result.build(null, resultCodeEnum);
    }

    //修改学期的开始时间和结束时间
    @PutMapping("/time")
    public Result<String> updateTerm(@RequestBody Term term) {
        termService.updateTerm(term);
        return Result.ok("修改成功");
    }

    //根据当前时间获取当前学期信息
    @GetMapping("/time")
    public Result<Term> getTermIdByTime() {
        Term term = termService.getTermIdByTime();
        return Result.ok(term);
    }

}
